package com.cs.beneficios.business.entity;

import com.cs.beneficios.business.model.AtributoVO;
import com.cs.beneficios.common.ConnectionFactory;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class ModificarBOTDAO {
	/**
	 * Conexion a la base de datos.
	 */
	private Connection conn = null;
	private ConnectionFactory cf;

	/**
	 * Constructor que inicializa la conexion con la base de datos.
	 * 
	 * @param conn
	 *            Conexion con la base de datos.
	 */

	public ModificarBOTDAO(ConnectionFactory cf) {
		this.cf = cf;
		try {
			this.conn = this.cf.getConnection();
		} catch (SQLException e) {
		}
	}

	public Collection GetAtributosSubpro(String codesubpro) throws SQLException {

		int codeSub = Integer.parseInt(codesubpro.trim());
		String query = "SELECT CODIGO, NOMBRE, TIPO_DATO, TIPO_ATRIBUTO "
				+ "FROM TCS_BEN_SUBP_ATR_BENEFICIARIO WHERE SBPR_CODIGO="
				+ codeSub + " " + "AND VIGENCIA='A' AND CAMPO_A2 IS NULL";
		List lAtributos = new LinkedList();
		ResultSet rst = null;
		rst = cf.getRecordsRset(query, null, conn);
		AtributoVO objatri;
		while (rst.next()) {
			objatri = new AtributoVO();
			objatri.setCodatributo(rst.getString("CODIGO"));
			objatri.setNombre(rst.getString("NOMBRE"));
			objatri.setTipodato(rst.getString("TIPO_DATO"));
			objatri.setTipoatributo(rst.getString("TIPO_ATRIBUTO"));
			lAtributos.add(objatri);
			objatri = null;
		}
		return lAtributos;
	}

	public String GetSedeOtorgamiento(String codeBot) throws SQLException {

		String sede = "";
		String query = "SELECT CODSEDE " + "FROM VCS_BENEFICIOS_OTORGADOS "
				+ "WHERE CODBOT=" + codeBot;

		ResultSet rst = null;
		rst = cf.getRecordsRset(query, null, conn);
		if (rst.next()) {
			return rst.getString("CODSEDE");
		}
		return sede;
	}

	public String Getbeneficiarioatri(String tipodato, String codebene,
			String codeatri) throws SQLException {

		String strSeleccion = "";
		String strValor = null;
		if (tipodato.equals("N")) {
			strSeleccion = "VALOR_NUM_ATR";
		} else if (tipodato.equals("F")) {
			strSeleccion = "TO_CHAR(VALOR_FEC_ATR,'DD/MM/YYYY')";
		} else {
			strSeleccion = "VALOR_CAR_ATR";
		}

		int codbeneficiario = Integer.parseInt(codebene.trim());
		int codatri = Integer.parseInt(codeatri.trim());
		String query = "SELECT " + strSeleccion
				+ " valor FROM TCS_BEN_BENEFICIARIO_ATRIBUTO "
				+ "WHERE SAB_CODIGO = " + codatri + " " + "AND BEN_CODIGO = "
				+ codbeneficiario;
		// System.out.println(query);
		ResultSet rst = null;
		rst = cf.getRecordsRset(query, null, conn);
		if (rst.next()) {
			strValor = rst.getString("valor");
		}
		return strValor;
	}

	public ResultSet Getbeneficioatri(String tipodato, String codebeneotor,
			String codeatri) throws SQLException {

		String strSeleccion = "";
		if (tipodato.equals("N")) {
			strSeleccion = "VALOR_NUM_ATR";
		} else if (tipodato.equals("F")) {
			strSeleccion = "TO_CHAR(VALOR_FEC_ATR,'DD/MM/RRRR')";
		} else {
			strSeleccion = "VALOR_CAR_ATR";
		}

		int codbeneotor = Integer.parseInt(codebeneotor.trim());
		int codatri = Integer.parseInt(codeatri.trim());
		String query = "SELECT " + strSeleccion
				+ " valor, codigo FROM TCS_BEN_BENEFICIO_ATRIBUTO "
				+ "WHERE BOT_CODIGO= " + codbeneotor + " "
				+ "AND SAB_CODIGO = " + codatri + " ";
		ResultSet rst = null;
		rst = cf.getRecordsRset(query, null, conn);
		return rst;
	}
}
